﻿/*
 * Copyright 2002-2010 Edward L. Stauff.                  Contact: <EdStauff@gmail.com>.
 * 
 * This file, "Open.cs", is part of the "Stauffware.Common.IO" class library.
 * "Stauffware.Common" is free software: you can redistribute it and/or 
 * modify it under the terms of the GNU Lesser Public License as published by the 
 * Free Software Foundation, either version 3 of the License, or (at your option) 
 * any later version.
 * 
 * "Stauffware.Common" is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser Public License for more 
 * details.
 * 
 * You should have received a copy of the GNU Lesser Public License along with 
 * the "Stauffware.Common" sources.  If not, see <http://www.gnu.org/licenses/>.
 */
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Stauffware.Common.Core;

namespace Stauffware.Common.IO
{
	/// <summary>
	/// Useful shortcut methods for common file open operations.
	/// </summary>
	public static class Open
	{
		//-----------------------------------------------------------------------------
		/// <summary>
		/// Creates a new StreamWriter based on a FileStream.
		/// </summary>
		/// <param name="path">the path of the file to open</param>
		/// <param name="mode">when to replace or append an existing file</param>
		/// <param name="sharing">what access to allow others</param>
		/// <returns>a StreamWriter backed by a FileStream</returns>

		public static StreamWriter FileStreamWriter (string path, FileMode mode,
															FileShare sharing)
		{
			FileStream fs = new FileStream(path, mode, FileAccess.Write, sharing);
			try
			{
				return new StreamWriter(fs);
			}
			catch (Exception)
			{
				fs.Close();
				throw;
			}
		}

		//-----------------------------------------------------------------------------
		/// <summary>
		/// Creates a new StreamWriter based on a FileStream.
		/// </summary>
		/// <param name="path">the path of the file to open</param>
		/// <param name="mode">when to replace or append an existing file</param>
		/// <param name="sharing">what access to allow others</param>
		/// <param name="encoding">the encoding to use</param>
		/// <returns>a StreamWriter backed by a FileStream</returns>

		public static StreamWriter FileStreamWriter (string path, FileMode mode,
											FileShare sharing, Encoding encoding)
		{
			FileStream fs = new FileStream(path, mode, FileAccess.Write, sharing);
			try
			{
				return new StreamWriter(fs, encoding);
			}
			catch (Exception)
			{
				fs.Close();
				throw;
			}
		}

		//-----------------------------------------------------------------------------
		/// <summary>
		/// Creates a StreamReader based on a FileStream.
		/// </summary>
		/// <param name="path">the path of the file to open</param>
		/// <param name="sharing">what access to allow others</param>
		/// <returns>a StreamReader backed by a FileStream</returns>

		public static StreamReader FileStreamReader (string path, FileShare sharing)
		{
			FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, sharing);
			try
			{
				return new StreamReader(fs);
			}
			catch (Exception)
			{
				fs.Close();
				throw;
			}
		}

		//-----------------------------------------------------------------------------
		/// <summary>
		/// Creates a StreamReader based on a FileStream.
		/// </summary>
		/// <param name="path">the path of the file to open</param>
		/// <param name="sharing">what access to allow others</param>
		/// <param name="encoding">the encoding to use</param>
		/// <returns>a StreamReader backed by a FileStream</returns>
		/// 
		public static StreamReader FileStreamReader (string path, FileShare sharing, 
																	Encoding encoding)
		{
			FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, sharing);
			try
			{
				return new StreamReader(fs, encoding);
			}
			catch (Exception)
			{
				fs.Close();
				throw;
			}
		}

		//-----------------------------------------------------------------------------
	}
}
